package userInterface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import error.ClassError;

public abstract class TextUserInterface {
	
	protected final TextOutput output;
	protected final ClassError logger;

	protected TextUserInterface() {
		this.output = new TextOutput(true);
		String class_name = this.getClass().getName();
		String error_dir = "error_logs";

		logger = new ClassError(class_name);		
		//logger = ErrorLogger.getLogger(error_dir, class_name, Level.ALL);
		
		if (ClassError.redirectErrStream(error_dir, class_name) == false){
			output.println("Error redirecting failed!");
		}	
	}

        protected void enableOutput(){
            output.setDisplay(true);
        }
        
        protected void disableOutput(){
            output.setDisplay(false);
        }

	public String getStringInput(String promptMessage) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		output.println(promptMessage + " : ");

		try {
			return br.readLine();
		} catch (IOException e) {
			logger.warning("Error when trying to get user input");
			return "";
		}
	}

	public int getIntInput(String promptMessage, int minValue, int maxValue) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		output.println(promptMessage + " (" + minValue + " - " + maxValue + "): ");

		String input;
		int integer = -1;
		boolean isInt = false;

		while (!isInt) {
			try {
				input = br.readLine();
			} catch (IOException e) {

				// logger.warning("Error when trying to get user input");
				// logger.log(Level.WARNING, "Stack Trace", e);
				return integer;
			}

			try {
				integer = Integer.parseInt(input);
				isInt = true;
			} catch (NumberFormatException e) {
				output.println("Please enter an integer between " + minValue + " and "
						+ maxValue + "!");
			}

		}

		return integer;
	}
	
	/*
	 * // Returns a String containing the current date and time.
	 * private String getDateTime() {
	 * DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	 * Date date = new Date();
	 * return dateFormat.format(date);
	 * }
	 */

	@Override
	public String toString() {
		return "TextUserInterface.";
	}

}
